package com.ruoyi.course.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.course.domain.Course;
import com.ruoyi.course.domain.CourseRecord;
import com.ruoyi.course.domain.CourseUserList;
import com.ruoyi.course.domain.SelectCourse;
import com.ruoyi.course.domain.dto.DeleteCourseStudentsDTO;
import com.ruoyi.course.service.ICourseRecordService;
import com.ruoyi.course.service.ICourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * @Author: @Weiney
 * @ClassName: StartCourseController
 * @Description: 开始上课Controller
 * @Create: 2025-02-26
 **/

@RestController
@RequestMapping("/course/startCourse")
public class StartCourseController extends BaseController {

    @Autowired
    private ICourseService courseService;
    @Autowired
    private ICourseRecordService courseRecordService;

    /**
     * 获取课程列表
     */
    @PreAuthorize("@ss.hasPermi('course:startCourse:list')")
    @GetMapping("/list")
    public AjaxResult getCourseList() {
        Course course = new Course();
        course.setStatus("0"); // 查询已开课的课程
        List<Course> courseList = courseService.selectCourseList(course);
        return success(courseList);
    }

    /**
     * 查询上课状态
     *
     * @param courseId
     * @return
     */
    @PreAuthorize("@ss.hasPermi('course:startCourse:isStarted')")
    @GetMapping(value = "/startStatus/{courseId}")
    public AjaxResult getCourseSelectionStatus(@PathVariable("courseId") Long courseId) {
        boolean isSelected = courseRecordService.getStartCourseStatus(courseId);
        return success(isSelected);
    }

    /**
     * 新增课程的上课记录
     */
    @PreAuthorize("@ss.hasPermi('course:startCourse:start')")
    @Log(title = "课程的上课记录", businessType = BusinessType.INSERT)
    @PostMapping("/start")
    public AjaxResult add(@RequestParam Long courseId)
    {
        CourseRecord courseRecord = new CourseRecord();
        courseRecord.setCourseId(courseId);
        courseRecord.setUserId(getUserId());
        courseRecord.setDeptId(getDeptId());
        courseRecord.setStatus(0L);
        courseRecord.setStartTime(DateUtils.getNowDate());
        courseRecord.setCreateBy(getUsername());
        return toAjax(courseRecordService.insertCourseRecord(courseRecord));
    }

    /**
     * 修改课程的上课记录：结束上课
     */
    @PreAuthorize("@ss.hasPermi('course:startCourse:end')")
    @Log(title = "课程的上课记录", businessType = BusinessType.UPDATE)
    @PutMapping("/end")
    public AjaxResult edit(@RequestParam Long courseId)
    {
        // 查询要结束上课的记录
        CourseRecord editRecord = courseRecordService.selectCourseRecordByCourseIdAndStatus(courseId);
        // 修改该条上课记录
        CourseRecord courseRecord = new CourseRecord();
        courseRecord.setId(editRecord.getId());
        courseRecord.setEndTime(DateUtils.getNowDate());
        courseRecord.setStatus(1L);

        return toAjax(courseRecordService.updateCourseRecord(courseRecord));
    }

    /**
     * 查询课程学生列表列表
     */
    @PreAuthorize("@ss.hasPermi('course:courseStudents:list')")
    @GetMapping("/courseStudents/list")
    public TableDataInfo userList(CourseUserList courseUserList)
    {
        startPage();
        List<CourseUserList> list = courseRecordService.selectCourseUserListList(courseUserList);
        return getDataTable(list);
    }

    /**
     * 导出课程学生列表列表
     */
    @PreAuthorize("@ss.hasPermi('course:courseStudents:export')")
    @Log(title = "课程学生列表", businessType = BusinessType.EXPORT)
    @PostMapping("/courseStudents/export")
    public void exportStudents(HttpServletResponse response, CourseUserList courseUserList)
    {
        List<CourseUserList> list = courseRecordService.selectCourseUserListList(courseUserList);
        ExcelUtil<CourseUserList> util = new ExcelUtil<CourseUserList>(CourseUserList.class);
        util.exportExcel(response, list, "课程学生列表数据");
    }

    /**
     * 获取课程学生列表详细信息
     */
    @PreAuthorize("@ss.hasPermi('course:courseStudents:query')")
    @GetMapping(value = "/courseStudents/{courseId}")
    public AjaxResult getStudentsInfo(@PathVariable("courseId") Long courseId)
    {
        return success(courseRecordService.selectCourseUserListByCourseId(courseId));
    }

    /**
     * 新增课程学生列表
     */
    @PreAuthorize("@ss.hasPermi('course:courseStudents:add')")
    @Log(title = "课程学生列表", businessType = BusinessType.INSERT)
    @PostMapping("/courseStudents")
    public AjaxResult addStudent(@RequestBody CourseUserList courseUserList)
    {
        return toAjax(courseRecordService.insertCourseUserList(courseUserList));
    }

    /**
     * 修改课程学生列表
     */
    @PreAuthorize("@ss.hasPermi('course:courseStudents:edit')")
    @Log(title = "课程学生列表", businessType = BusinessType.UPDATE)
    @PutMapping("/courseStudents")
    public AjaxResult editStudent(@RequestBody CourseUserList courseUserList)
    {
        return toAjax(courseRecordService.updateCourseUserList(courseUserList));
    }

    /**
     * 删除课程学生列表
     */
    @PreAuthorize("@ss.hasPermi('course:courseStudents:remove')")
    @Log(title = "课程学生列表", businessType = BusinessType.DELETE)
    @DeleteMapping("/courseStudents")
    public AjaxResult removeStudents(@RequestBody DeleteCourseStudentsDTO deleteCourseStudentsDTO)
    {
        // 从请求体中获取 courseId 和 userIds
        Long courseId = deleteCourseStudentsDTO.getCourseId();
        List<Long> userIds = deleteCourseStudentsDTO.getUserIds();
        return toAjax(courseRecordService.deleteCourseUserListByCourseIds(courseId, userIds));
    }
}